import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebPage5 extends StatefulWidget {
  const WebPage5({super.key});

  @override
  State<WebPage5> createState() => _WebPage5State();
}

class _WebPage5State extends State<WebPage5> {
  // 1
  late WebViewController controller;

  @override
  void initState() {
    super.initState();
    _initWebView();
  }

  _initWebView() async {
    // 2
    controller = WebViewController();
    // controller.loadRequest(Uri.parse("https://flutter.cn"));
    controller.loadFlutterAsset("assets/html/index.html");
  }

  @override
  Widget build(BuildContext context) {
    return WebViewWidget(controller: controller);
    // return Column(
    //   children: [
    //     Expanded(child: InnerScrollView())
    //   ],
    // );
  }
}

class MyWebViewWidget extends StatefulWidget {
  final String url;
  const MyWebViewWidget({super.key, required this.url});

  @override
  State<MyWebViewWidget> createState() => _MyWebViewWidgetState();
}

class _MyWebViewWidgetState extends State<MyWebViewWidget> {
  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

typedef void ChangeStatusBarStyle(List<dynamic> args);
typedef void BackToCloseWeb(List<dynamic> args);
typedef Future GetLikeDetails();
typedef Future RequestAddLike();
typedef Future<String?> SaveImageByFlutter(List<dynamic> args);

class JsSDKOptions {
  BuildContext context;
  String userAgent;
  String type;
  String shareTitle;
  String shareDesc;
  String sharePicUrl;
  String shareUrl;
  String articleUrl;
  int publishId;
  int pushId;
  int serviceId;
  String columnId;
  String columnName;

  ChangeStatusBarStyle changeStatusBarStyle;
  BackToCloseWeb backToCloseWeb;
  GetLikeDetails getLikeDetails;
  RequestAddLike requestAddLike;
  SaveImageByFlutter saveImageByFlutter;

  JsSDKOptions({
    required this.context,
    required this.userAgent,
    required this.type,
    required this.shareTitle,
    required this.shareDesc,
    required this.sharePicUrl,
    required this.articleUrl,
    required this.shareUrl,
    required this.publishId,
    required this.pushId,
    required this.serviceId,
    required this.columnId,
    required this.columnName,
    required this.changeStatusBarStyle,
    required this.backToCloseWeb,
    required this.getLikeDetails,
    required this.requestAddLike,
    required this.saveImageByFlutter,
  });
}

class JsSDKFactory {
  static const String PLATFORM_TIANMU = "tianmu";
  static JsSDKFactory? _instance;
  static late JsSDKOptions options;
  static late WebViewController controller;

  JsSDKFactory._internal() {
    _instance = this;
  }

  factory JsSDKFactory() => _instance ?? JsSDKFactory._internal();

  static init(
      WebViewController webViewController, JsSDKOptions jsSDKOptions) {
    controller = webViewController;
    options = jsSDKOptions;
  }

  assembleController() {
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    controller.addJavaScriptChannel(
        'jumpToNews',
        onMessageReceived: (JavaScriptMessage message) {
          Map<String, dynamic> body = json.decode(message.message);
          if (body.isNotEmpty) {
            String? id = '${body['id']}';
            _requestDetilsToJump(int.parse(id));
          } else {
            print("跳转异常，请重试");
          }
        });
  }

  void _requestDetilsToJump(int publishId) {
    Navigator.of(options.context).push(
      MaterialPageRoute(
        builder: (context) => const MyWebViewWidget(
          url:
              'https://h5.jcy.jinhua.com.cn/folder1342/folder1343/folder1358/folder1378/folder2467/folder2470/2024-07-03/PwpZ5lG1oBJ2f6Cn.html?v=2',
        ),
      ),
    );
  }
}
